//
//  ButtonControlM.m
//  
//  jhf
//
//  Created by Mac on 2020/7/30.
//

#import "ButtonControlM.h"
#import <CooperSDK/UIColor+BFPaperColors.h>
#import <CooperSDK/CALayer+Addition.h>
#import <CooperSDK/JDefine.h>

@implementation ButtonControlM

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self defaultConfig];
    }
    return self;
}

- (void)defaultConfig {
    
    self.btTitleFont = [UIFont systemFontOfSize:15];
    //    model.textTextAlignment = NSTextAlignmentLeft;
    self.btTitleColorHex = @"#333333";
    self.btEnable = true;
    self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.vLeftCons = 15;
    self.vRadius = 22;
    self.vHeightCons = 44;
    self.vWidthCons = kScreen_Width - 30;
    
    self.method = @"rowBTTap";
    self.btEnable = true;
    self.vTopCons = 12.0;
    self.vBottomCons = 12.0;
    
    self.imageVcontentMode = UIViewContentModeScaleAspectFit;
    
}

- (BOOL)isEnableTap; {
    if (self.lastTapTime == 0) {
        self.lastTapTime = [NSDate date].timeIntervalSince1970;
        return true;
    }
    if ([NSDate date].timeIntervalSince1970 - self.lastTapTime < 1) {
        NSLog(@"重复点击 已拒绝");
        return false;
    }
    self.lastTapTime = [NSDate date].timeIntervalSince1970;
    return true;
}

+ (ButtonControlM *)defaultBTText:(NSString *)text; {
    ButtonControlM *model = [[ButtonControlM alloc] init];
    /// 显示
    model.btTitle = text;
    return model;
}


+ (ButtonControlM *)defaultImageName:(NSString *)imageName; {
    ButtonControlM *model = [[ButtonControlM alloc] init];
    /// 显示
    model.btBgImageName = imageName;
    return model;
}

- (void)controlBT:(UIButton *)btn; {
    [self controlV:btn];
    [btn.imageView setContentMode:self.imageVcontentMode];
    [btn setTitle:self.btTitle forState:0];
    btn.titleEdgeInsets = self.titleEdgeInsets;
    if (self.btTitleColorHex && self.btTitleColorHex.length > 5) {
        [btn setTitleColor:[UIColor colorWithHexString:self.btTitleColorHex] forState:0];
    }
    if (self.btTitleFont) {
        btn.titleLabel.font = self.btTitleFont;
    }
    if (self.btBgImageName) {
        [btn setBackgroundImage:[UIImage imageNamed:self.btBgImageName] forState:0];
    }
}

+ (double)ideaHeight:(NSArray <ButtonControlM *>*)btns; {
    __block CGFloat leftCons = 0.f;
    __block CGFloat topMaxCons = 0.f;
    __block CGFloat heightMaxCons = 0.f; // 本行最高的按钮
    __block CGFloat floorHeightCons = 0.f; //上次顶部高度
    __block double ideaHeight = 0.0; // 最终高度
    [btns enumerateObjectsUsingBlock:^(ButtonControlM *  _Nonnull config, NSUInteger idx, BOOL * _Nonnull stop) {
        // 排放位置
        // 左侧边距
        leftCons += config.vLeftCons;
        CGRect frame = CGRectMake(leftCons, config.vTopCons + floorHeightCons, config.vWidthCons, config.vHeightCons);
        // 顶部距离 最大
        topMaxCons = MAX(topMaxCons, config.vTopCons);
        //最高的btn
        heightMaxCons = MAX(config.vHeightCons, heightMaxCons);
        if ((leftCons + config.vWidthCons) > kScreen_Width && idx > 0) {
            // 左侧边距+要添加按钮 大于背景宽度 且不是第一个 需要换行
            leftCons = config.vLeftCons;
            floorHeightCons = floorHeightCons + topMaxCons + heightMaxCons;
            frame = CGRectMake(leftCons, floorHeightCons + config.vTopCons, config.vWidthCons, config.vHeightCons);
            //设置新的 最高边距和最高按钮高度<新的一行 第一个>
            topMaxCons = config.vTopCons;
            heightMaxCons = config.vHeightCons;
        }
        // 设置frame后 增加 左侧宽度到 按钮的右侧
        leftCons += config.vWidthCons;
        
        // 更新按钮 frame
        config.frame = frame;
        
        if (idx == btns.count - 1) {
            /// 最后一个判断cell的高度刷新
            ideaHeight = floorHeightCons + (topMaxCons == 0 ? (config.vHeightCons + config.vTopCons) : (topMaxCons + heightMaxCons))  + config.vBottomCons;
        }
    }];
    return ideaHeight;
}

@end
